package net.stogi.common.security.spring;

import java.util.ArrayList;
import java.util.Collection;

import net.stogi.common.security.dto.PrincipalData;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;

public class PrincipalUserDetails implements UserDetails {

	private static final long serialVersionUID = -5213829456389535955L;

	private PrincipalData principalData;

	public PrincipalUserDetails() {}

	public PrincipalUserDetails(PrincipalData principalData) {
		this.principalData = principalData;
	}

	@Override
	public Collection<GrantedAuthority> getAuthorities() {
		ArrayList<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

		for (String role : getPrincipalData().getRoles()) {
			authorities.add(new GrantedAuthorityImpl(role));
		}

		return authorities;
	}

	@Override
	public String getPassword() {
		return getPrincipalData().getPassword();
	}

	/**
	 * @return the principalData
	 */
	public PrincipalData getPrincipalData() {
		return principalData;
	}

	@Override
	public String getUsername() {
		return getPrincipalData().getUsername();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}
}
